<template>
  <a-card :bordered="false">
    <!-- <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="10">
            <a-col :md="4" :sm="24">
              <a-form-item label="查询类别">
                <a-select allowClear v-model="queryParam.condition">
                  <a-select-option key="CustomerName">客户名称</a-select-option>
                  <a-select-option key="DocumentNo">单据号</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="4" :sm="24">
              <a-form-item>
                <a-input v-model="queryParam.keyword" placeholder="关键字" />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-button
                type="primary"
                @click="
                  () => {
                    this.pagination.current = 1
                    this.getDataList()
                  }
                "
                >查询</a-button
              >
              <a-button style="margin-left: 8px" @click="repossess">重置</a-button>
            </a-col>
          </a-row>
        </a-form>
      </div> -->

    <a-table
      ref="table"
      :scroll="{ y: 530 }"
      :columns="columns"
      :rowKey="(row, index) => row.Id"
      :dataSource="data"
      :pagination="pagination"
      :loading="loading"
      @change="handleTableChange"
      :bordered="true"
      size="small">
      <span slot="CreateTime" slot-scope="CreateTime">{{ timeFormat(CreateTime) }}</span>
      <template slot="ApprovalStatus" slot-scope="text">
        <a-tag color="green" v-if="text == 1">
          审批通过
        </a-tag>
        <a-tag color="blue" v-if="!text">
          未审批
        </a-tag>
        <a-tag color="red" v-if="text == 2">
          审批驳回
        </a-tag>
      </template>
      <span slot="action" slot-scope="text, record">
        <template>
          <a v-if="record.ApprovalStatus" @click="Check(record.Id)">查看</a>
          <!-- <a-divider v-if="record.ApprovalStatus" type="vertical" /> -->
          <a v-if="hasPerm('SPL_SampleApplyMain.Edit') && !record.ApprovalStatus" @click="handleEdit(record.Id)"
          >编辑</a
          >
          <a-divider
            v-if="
              hasPerm('SPL_SampleApplyMain.Edit') && !record.ApprovalStatus && hasPerm('SPL_SampleApplyMain.Delete')
            "
            type="vertical"
          />
          <a-divider v-if="record.ApprovalStatus == 2" type="vertical" />
          <a v-if="record.ApprovalStatus == 2" @click="handleRevise(record.Id)">修改</a>
          <a v-if="hasPerm('SPL_SampleApplyMain.Delete') && !record.ApprovalStatus" @click="handleDelete([record.Id])"
          >删除</a
          >
        </template>
      </span>
    </a-table>
    <edit-form ref="editForm" :disabled="disabled" :parentObj="this"></edit-form>
  </a-card>
</template>

<script>
import EditForm from '../../../SampleManagement/MySample/EditForm.vue'
import moment from 'moment'

const columns = [
  { title: '客户名称', dataIndex: 'CustomerName', width: '15%' },
  { title: '单据号', dataIndex: 'DocumentNo' },
  {
    title: '申请日期',
    dataIndex: 'CreateTime',
    align: 'center',
    scopedSlots: { customRender: 'CreateTime' }
  },
  // { title: '销售经理', dataIndex: 'AccountManager' },
  // { title: '组织机构', dataIndex: 'Department' },
  { title: '经办人', dataIndex: 'Transactor' },
  { title: '收货地址', dataIndex: 'DeliverAddress', width: '20%' },
  // { title: '快递单号', dataIndex: 'TrackingNumber' },
  {
    title: '审批状态',
    dataIndex: 'ApprovalStatus',
    align: 'center',
    scopedSlots: { customRender: 'ApprovalStatus' }
  },
  { title: '操作', dataIndex: 'action', align: 'left', scopedSlots: { customRender: 'action' } }
]

export default {
  components: {
    EditForm
  },
  props: {
    name: { require: true, type: String }
  },
  mounted () {
    this.queryParam.condition = 'CustomerName'
    this.queryParam.keyword = this.name
    this.getDataList()
  },
  data () {
    return {
      data: [],
      visible: false,
      disabled: false,
      pagination: {
        current: 1,
        pageSize: 25,
        showTotal: (total, range) => `总数:${total} 当前:${range[0]}-${range[1]}`,
        pageSizeOptions: ['25', '50', '100'], // 设置pageSize的可选值，页面可以通过下拉框进行选择
        showSizeChanger: true
      },
      filters: {},
      sorter: { field: 'Id', order: 'desc' },
      loading: false,
      columns,
      queryParam: {
        condition: 'CustomerName'
      },
      selectedRowKeys: []
    }
  },
  methods: {
    openForm (name, title) {
      console.log(name)
      this.title = title
      this.visible = true
      this.data = []
      this.name = name
      this.queryParam.condition = 'CustomerName'
      this.queryParam.keyword = name
      this.getDataList()
    },
    CheckboxProps (record) {
      return {
        props: {
          disabled: record.ApprovalStatus == 1
        }
      }
    },
    handleRevise (id) {
      this.disabled = false
      this.$refs.editForm.openForm(id, '修改')
    },
    repossess () {
      this.queryParam = { condition: 'CustomerName' }
      this.getDataList()
    },
    handleTableChange (pagination, filters, sorter) {
      this.pagination = { ...pagination }
      this.filters = { ...filters }

      this.getDataList()
    },
    getDataList () {
      this.selectedRowKeys = []
      this.loading = true
      this.$http
        .post('/SampleManagement/SPL_SampleApplyMain/GetDataListByMySelfAndMoreCondition', {
          PageIndex: this.pagination.current,
          PageRows: this.pagination.pageSize,
          SortField: this.sorter.field || 'Id',
          SortType: this.sorter.order,
          Search: [this.queryParam],
          ...this.filters
        })
        .then(resJson => {
          this.loading = false
          this.data = resJson.Data
          if (this.data) {
            const pagination = { ...this.pagination }
            pagination.total = resJson.Total
            this.pagination = pagination
          }
        })
    },
    onSelectChange (selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    hasSelected () {
      return this.selectedRowKeys.length > 0
    },
    handleAdd () {
      this.disabled = false
      this.$refs.editForm.openForm(null, '新建样品申请表信息')
    },
    handleEdit (id) {
      this.disabled = false
      this.$refs.editForm.openForm(id, '编辑样品申请表信息')
    },
    Check (id) {
      this.disabled = true
      this.$refs.editForm.openForm(id, '查看')
    },

    handleDelete (ids) {
      var thisObj = this
      this.$confirm({
        title: '确认删除吗?',
        onOk () {
          return new Promise((resolve, reject) => {
            thisObj.$http.post('/SampleManagement/SPL_SampleApplyMain/DeleteDataOfAll', ids).then(resJson => {
              resolve()

              if (resJson.Success) {
                thisObj.$message.success('操作成功!')

                thisObj.getDataList()
              } else {
                thisObj.$message.error(resJson.Msg)
              }
            })
          })
        }
      })
    },
    timeFormat (val) {
      // 时间格式化函数
      return moment(val).format('YYYY-MM-DD')
    }
  }
}
</script>
